<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>帮助文档管理</title>
<link href="${ctx}/jeasyui-extensions/plugins/uploadify/uploadify.css" rel="stylesheet">
<script type="text/javascript" src="${ctx}/jeasyui-extensions/plugins/uploadify/jquery.uploadify.js"></script>
<script type="text/javascript" src="${ctx}/jeasyui-extensions/jeasyui-extensions/jquery-easyui-euploadify/jquery.euploadify.js"></script>
<link href="${ctx}/jeasyui-extensions/plugins/ueditor/ue1_4_3-utf8-net/themes/default/css/ueditor.css" rel="stylesheet">
<script type="text/javascript" src="${ctx}/jeasyui-extensions/plugins/ueditor/ue1_4_3-utf8-net/ueditor.config.js"></script>
<script type="text/javascript" src="${ctx}/jeasyui-extensions/plugins/ueditor/ue1_4_3-utf8-net/ueditor.all.js"></script>
<script type="text/javascript" src="${ctx}/jeasyui-extensions/plugins/ueditor/ue1_4_3-utf8-net/lang/zh-cn/zh-cn.js"></script>
<script type="text/javascript" src="${ctx}/jeasyui-extensions/jeasyui-extensions/jquery-easyui-ueditor/jquery.ueditor.js"></script>

<script type="text/javascript">
	var joinFileId;
	var $tree;
	var msgs;
	var links;
	var delLinks;
	$(function() {
		$tree = $("#tree").tree(
				{
					method : "get",
					lines : true,
					url : "${ctx}/basic/helpDocument/tree/",
					dataPlain : true, //该属性用以启用当前 easyui-tree 控件对平滑数据格式的支持
					toggleOnClick : false,
					checkbox : true,
					onSelect : function(node) {
						$("#fm").form("clear");
						$("#content_fm").form("clear");

						$.post("${ctx}/basic/helpDocument/content", {
							id : node.id
						}, function(data) {
							if (data.success) {
								$('#fm').form('load', {
									id : data.obj.id,
									helpDocTitle : data.obj.helpDocTitle
								});
								data.obj.helpDocType
										&& fn_setValueForCombo("helpDocType", data.obj.helpDocType.id, data.obj.helpDocType.codeName);
							} else {
								$.messager.show({
									icon : "error",
									msg : data.msg
								});
							}
						}, "json");

						/* $('#fm').form('load', $.extend(node.attributes, {
							pbsParentNode : node.id
						})); */

						$.post("${ctx}/basic/documentContent/content", {
							helpDocId : node.id
						}, function(data) {
							if (data.success) {
								$('#content_fm').form('load', {
									id : data.obj.id,
									helpDocId : data.obj.helpDocId,
									helpDocTag : data.obj.helpDocTag,
									helpDocContent : data.obj.helpDocContent,
									basicFileList : data.obj.basicFileList
								});
								var basicFileList = data.obj.basicFileList;
								$("#fujian tr").remove();
								for (var i = 0; i < basicFileList.length; i++) {
									$("#fujian").prepend(
											"<tr><td><a id="+basicFileList[i].id+" href='#'>" + basicFileList[i].fileName
													+ "</a>&nbsp;&nbsp;&nbsp;" + basicFileList[i].fileSize + " KB&nbsp;<span id="+basicFileList[i].id+"><a href='#'>删除</a></span></td></tr>");
								}
								links = document.body.getElementsByTagName("a");
								delLinks = document.body.getElementsByTagName("span");
								if (links != null) {
									for (var i = 0; i < links.length; i++) {
										links[i].onclick = function() {
											var fileId = this.id;
											var url = "${ctx}/basic/basicFile/download/" + fileId;
											window.parent.location.href = url;
										}
									}
								}
								if(delLinks!=null){
									for(var j = 0;j<delLinks.length;j++){
										delLinks[j].onclick=function(){
											var fileId = this.id;
											if (oldFileId) {
												$.get("${ctx}/basic/documentContent/delBasicFile", {
													basicFileId : fileId
												}, function(data) {
													if (data.success) {
														//移除附件
														$(this).parent("tr").remove();
														$.messager.show({
															title : "信息",
															width : 300,
															height : 100,
															msg : data.msg,
															icon : "info",
															position : "bottomRight"
														});
													}

													
												}, "json");
											}
										}
									}
								}
							} else {
								$('#content_fm').form('load', {
									helpDocId : node.id
								});
								$.messager.show({
									icon : "error",
									msg : data.msg
								});
							}
						}, "json");

						/* var basicFileList = $("#basicFileList").val();
						alert(basicFileList);
						if(basicFileList){
							for(var i = 0; i<basicFileList.size();i++){
								alert(basicFileList[i].fileName);
							}
						} */

					},
					onContextMenu : function(e, node) {
						e.preventDefault();
						// 查找节点
						$tree.tree('select', node.target);
					},
					contextMenu : [ {
						text : "添加子节点",
						iconCls : "icon-standard-add",
						disabled : false,
						handler : function() {
							var snode = $tree.tree("getSelected");
							$tree.tree('expand', snode.target);
							addChild();
						}
					}, {
						text : "添加兄弟节点",
						iconCls : "icon-standard-add",
						disabled : false,
						handler : function() {
							addBrother();
						}
					}, "-", {
						text : "全部勾选",
						iconCls : "icon-metro-checkbox-checked",
						disabled : false,
						handler : function() {
							checkAll($tree);
						}
					}, {
						text : "全部取消",
						iconCls : "icon-metro-checkbox-unchecked",
						disabled : false,
						handler : function() {
							unCheckAll($tree);
						}
					} ]

				});

		fn_initCombo("helpDocType", "helpDocType");//帮助文档类型

		$("#addRoot").click(function() {
			addRoot();
		});

		$("#delete").click(function() {
			multiDeleteTreeNode($tree, "${ctx}/basic/helpDocument/delete/");
			$("#fm").form("clear");
			$("#content_fm").form("clear");
		});
		$("#save").click(function() {
			var msg1 = "";
			var msg2 = "";
			//var msg;
			$("#fm").form("submit", {
				url : "${ctx}/basic/helpDocument/update",
				success : function(data) {
					var node = $tree.tree('getSelected');
					if (node) {
						var fmData = $("#fm").form("getData");
						$tree.tree('update', {
							target : node.target,
							text : fmData.helpDocTitle,
							attributes : fmData
						});
						$tree.tree("select", node.target);
					}
					msg1 = "文档标题：" + data.msg;
					/* $.messager.show({
						msg : data.msg,
						icon : "info",
						position : "bottomRight"
					}); */

				}

			});
			$("#content_fm").form("submit", {
				url : "${ctx}/basic/documentContent/update",
				success : function(data) {
					msg2 = msg1 + ";文档内容：" + data.msg;
					$.messager.show({
						msg : msg2,
						icon : "info",
						position : "bottomRight"
					});
				}
			});

		});

		$("#content_del").click(function() {
			$.messager.confirm("您确定要进行要彻底删除该文档内容吗？", function(c) {
				if (c) {
					$.get("${ctx}/basic/helpDocument/delete/" + id, function(result) {
						$.messager.show({
							msg : result.msg,
							icon : "info",
							position : "bottomRight"
						});
						if (result.success) {
							$grid.datagrid("load");
						}
					}, 'json');
				}
			});
		});

		$("#fileId").euploadify({
			width : 820,
			multiTemplate : 'simple',
			multi : true,
			auto : false,
			showStop : true,
			showCancel : true,
			swf : '${ctx }/static/uploadify/uploadify.swf',
			uploader : '${ctx }/basic/basicFile/upload',
			onSelect : function(file) {
				debugger;
				if (joinFileId) {
					$("#fileId").euploadify('setFormData', {
						'joinFileId' : joinFileId
					});
					$("#joinFileId").val(joinFileId);
				} else {
					$.post("${ctx}/basic/basicJoinFile/update", {
						codeName : "帮助文档"
					}, function(data) {
						joinFileId = data.obj ? data.obj.id : "";
						$("#joinFileId").val(joinFileId);
						$("#fileId").euploadify('setFormData', {
							'joinFileId' : joinFileId
						});
						//showInfoForCallBack(data);
					}, "json");
				}
			}
		});

		/* $("#content_save").click(function() {
			$("#content_fm").form("submit", {
				url : "${ctx}/basic/documentContent/update",
				success : function(data) {
					$.messager.show({
						msg : data.msg,
						icon : "info",
						position : "bottomRight"
					});
				}
			});
		}); */

	});

	function addRoot() {
		$.post("${ctx}/basic/helpDocument/update", {
			helpParentId : "",
		}, function(data) {
			if (data.success) {
				$("#tree").tree({
					url : "${ctx}/basic/helpDocument/tree/"
				});

				//$tree.tree("select", $tree.tree('find', data.obj.id).target);
			} else {
				$.messager.show({
					icon : "error",
					msg : data.msg
				});
			}

		}, "json");
	}

	function fun_help_save() {
		$("#help_fm").form("submit", {
			url : "${ctx}/basic/helpDocument/update",
			success : function(result) {
				if (result.success) {
					$("#helpdlg").dialog("close");
					$("#tree").tree({
						url : "${ctx}/basic/helpDocument/tree/"
					});
				}
				showInfo(result.msg);
			}
		});
	}

	function addChild() {
		var node = $tree.tree("getSelected");
		if (node == null) {
			$.messager.alert("操作提醒", "请先选中节点", "warning");
			return false;
		}

		/* $("#help_dlg").dialog("open").dialog("setTitle", "新增根节点菜单");
		$("#help_fm").form("clear").form("load", {
			helpParentId : node.id
		}); */

		$.post("${ctx}/basic/helpDocument/update", {
			helpParentId : node.id
		}, function(data) {
			if (data.success) {
				$tree.tree('append', {
					parent : node.target,
					data : [ {
						id : data.obj.id,
						text : data.obj.text,
						attributes : $.extend(data.obj.attributes, {
							helpParentId : node.id
						})
					} ]
				});
				$tree.tree("select", $tree.tree('find', data.obj.id).target);
			} else {
				$.messager.show({
					icon : "error",
					msg : data.msg
				});
			}
		}, "json");
	}
	function addBrother() {
		var node = $tree.tree("getSelected");
		if (node == null) {
			$.messager.alert("操作提醒", "请先选中节点", "warning");
			return false;
		}
		$.post("${ctx}/basic/helpDocument/update", {
			pbsParentNode : node.pid
		}, function(data) {
			if (data.success) {
				$tree.tree('insert', {
					after : node.target,
					data : [ {
						id : data.obj.id,
						text : data.obj.text,
						attributes : $.extend(data.obj.attributes, {
							pbsParentNode : node.id
						})
					} ]
				});
				$tree.tree("select", $tree.tree('find', data.obj.id).target);
			} else {
				$.messager.show({
					icon : "error",
					msg : data.msg
				});
			}
		}, "json");
	}
	function splitCode(origin) {
		if (origin.length <= 2) {
			return [ origin, "" ];
		} else {
			return [ origin.substring(0, 2), origin.substring(2) ];
		}
	}
</script>
</head>

<body>
	<div data-options="region:'center',fit:true,border:false">
		<div id="cc" class="easyui-layout" data-options="fit:true">
			<div data-options="region:'west',split:true,title:'帮助文档'" style="padding: 1px; width: 230px;">
				<div id="tt" class="row" style="background:#eee">
				<div class="col-md-12">
					<button id="addRoot" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-plus"></span> 新增
				</button>
				<button id="delete" type="button" class="btn btn-danger">
					<span class="glyphicon glyphicon-minus"></span> 删除选中
				</button>
				</div>
				</div>
				<div><ul id="tree">
				</ul></div>
				

				<!-- <div id="help_dlg" class="easyui-dialog" style="width: 300px; height: 280px; padding: 10px 20px"
					data-options="closed:true,buttons:'#dlg-buttons'">
					<form id="help_fm" method="post">
						<input id="id" type="hidden" name="id" />
						<input id="helpParentId" type="hidden" name="helpParentId" />
						<table id="tbl">
							<tr>
								<td><label>文档标题:</label></td>
								<td><input name="helpDocTitle" class="easyui-validatebox"
									data-options="required: true"></td>
							</tr><tr>
								<td><label>文档类型:</label></td>
								<td><input id="helpDocType" name="helpDocType"></td>
							</tr>
					   </table>
					</form>
				</div>
				<div id="dlg-buttons">
					<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="fun_help_save()">保存</a>
					<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="javascript:$('#hlep_dlg').dialog('close')">取消</a>
				</div> -->
			</div>
			<div data-options="region:'center',title:'文档内容',fit:true,border:false" style="padding: 0px;">
				<!--<div id="rr" class="easyui-layout" data-options="fit:true">
					 <div data-options="region:'north',title:'编辑',split:true"
						style="height: 170px"> -->
				<div class="row" style="background:#eee">
				<div class="col-md-12">
				<button id="save" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-save"></span> 新增
				</button>
				</div>
				</div>
				<form id="fm" method="post">
					<input type="hidden" name="id" />
					<table id="tbl">
						<tr>
							<td><label>文档标题:</label></td>
							<td><input name="helpDocTitle" class="easyui-validatebox" data-options="required: true"></td>
							<td><label>文档类型:</label></td>
							<td><input id="helpDocType" name="helpDocType"></td>
						</tr>
					</table>

				</form>

				<!-- <div id="content" data-options="region:'center',title:'帮助文档明细'"
						style="padding: 0px;">

						<div id="cc" class="easyui-toolbar">
							<a id="content_save" class="easyui-linkbutton easyui-tooltip"
								data-options="plain: true, iconCls: 'icon-standard-disk',content:'保存'">保存</a>
						</div> -->
				<form id="content_fm" method="post">
					<input type="hidden" name="id" /> <input type="hidden" name="helpDocId" />
					<table id="tbl">
						<tr>
							<td><label>文档标签:</label></td>
							<td><input name="helpDocTag" value="" class="easyui-validatebox" data-options="required: true"></td>
						</tr>
						<tr>
							<td>附件：</td>
							<td height="50px"><input id="fileId" name="fileId" type="text" /><input id="joinFileId" name="joinFileId" type="hidden" /></td>
						</tr>

						<tr>
							<td><input type="hidden" id="oldFileId" value="${joinFileId}" /><input type="hidden" id="basicFileList" name="basicFileList" /></td>
							<td>
								<table id="fujian">
									<%-- <c:forEach items="${basicFileList}" var="bfile">
										<tr>
											<td>&nbsp;</td>
											<td><input type="hidden" id="fileId" name="fileId" value="${ bfile.id}" /></td>
											<td><a id="${ bfile.id}" href="#">${bfile.fileName}</a>&nbsp;&nbsp;&nbsp;<span>${bfile.fileSize} KB</span></td>
										</tr>
									</c:forEach> --%>
								</table>
							</td>

						</tr>

						<tr>
							<td style=""><label>内容：</label></td>
							<td colspan="2"><textarea name="helpDocContent" class="easyui-ueditor"
									data-options=" required: true, height: 400,width:820, value: '',
                              template: 'full', codeMirrorJsUrl: false, codeMirrorCssUrl: false"></textarea></td>
						</tr>
					</table>
				</form>
			</div>

			<!-- </div>
				</div> 

			</div>-->

		</div>
	</div>
</body>
</html>
